<?php

$baseDir = dirname(dirname(dirname(dirname(__FILE__))));
set_include_path(get_include_path().PATH_SEPARATOR.$baseDir.PATH_SEPARATOR.$baseDir.'/incubator');

include '../../testingfunctions.php';
include 'Pone/Pone_Exception.php';
include 'Pone/Database/Pone_Database_ConnectionFactory.php';
include_once './dbconfig.php'; // $config is defined here
include_once './test_setup.php'; // refreshTestData() is defined here

try
{
    refreshTestData($config);
    // /home/web/windows.php.net/docroot/downloads/snaps/
    $conn = Pone_Database_ConnectionFactory::getConnection($config);
    $stmt = $conn->createStatement();

    // Conditions as an array
    $deleteCond  = array(
      'id'       => '> 999999',
    );

    $affectedRowCount = $stmt->delete('test2', $deleteCond);
    assertTrue($affectedRowCount === 0, 'Expected: No row is affected because the target ID given does not exist intentionally: 999999');
    echo '<br />';

    // Conditions as an array
    $deleteCond  = array(
      'id'       => '= 4',
    );
    $affectedRowCount = $stmt->delete('test2', $deleteCond);
    assertTrue($affectedRowCount === 1, 'Expected: One row is deleted: ID = 4');
    echo '<br />';

    // Conditions as a string
    $deleteCond  = 'id = 5 AND username = \'pcdinh5\'';
    $affectedRowCount = $stmt->delete('test2', $deleteCond);
    assertTrue($affectedRowCount === 1, 'Expected: One row is deleted: ID = 5');
    echo '<br />';

    $rs = $stmt->fetchAll('SELECT * FROM test2 WHERE id = 5');
    assertTrue(count($rs) === 0, 'Expected: One row with ID field of 5 can not be found because it is deleted. ');
    // Free resources
    $stmt->close();
}
catch (Pone_Exception_DatabaseAccess $ex)
{
    echo implode('. ', $ex->getTraceMessages());
    echo '<br />';
    echo $ex->getNativeMessage();
}
catch (Exception $ex)
{
    echo $ex->getMessage();
}

$conn->close();
?>